package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.SearchPersonActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.SearchPersonResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

public class SearchPersonActivity extends BaseActivity {
    private SearchPersonActivityCustom dataBinding;
    private SlimAdapterEx<SearchPersonResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_search_person);
        initTopTitleBar(View.VISIBLE, "搜索人员");
    }

    @Override
    public void initData() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_10, new SlimInjector<SearchPersonResultBean>() {
                @Override
                public void onInject(SearchPersonResultBean data, IViewInjector injector) {
                    injector.text(R.id.tv_upper_left_content, data.getName())
                            .text(R.id.tv_upper_right_content, data.getCompany())
                            .text(R.id.tv_down_right_content, data.getDepartment() + " " + data.getPost())
                            .text(R.id.tv_down_left_content, data.getPhone())
                            .imageCircle(R.id.iv_head, data.getHeadImage())
                            .invisible(R.id.iv_enter);
                }
            }).attachTo(dataBinding.recyclerView);
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                        @Override
                        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                            Intent intent = new Intent();
                            intent.putExtra("bean", slimAdapterEx.getDataItem(position));
                            setResult(Config.SUCCESS, intent);
                            finish();
                        }
                    });
            dataBinding.floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
                @Override
                public void onSuggestionClicked(SearchSuggestion searchSuggestion) {

                }

                @Override
                public void onSearchAction(String currentQuery) {
                    refreshData(currentQuery);

                }
            });
        }
    }

    private void refreshData(String body) {
        OkGo.<HttpResult<List<SearchPersonResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.SEARCH_PERSON)
                .params("para", body)
                .execute(new DialogCallBack<HttpResult<List<SearchPersonResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<SearchPersonResultBean>>> response) {
                        HttpResult<List<SearchPersonResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
